package jp.co.toshiba.ffsa.service.cses;

import java.util.List;

import jp.co.toshiba.ffsa.entity.Price;

import org.seasar.extension.jdbc.JdbcManager;
import org.seasar.framework.container.SingletonS2Container;
import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
import org.seasar.struts.util.MessageResourcesUtil;

/**
 * 簡易(通常入力)ビジネスロジック
 * @author npc
 */
public class Cses102Service {
    private String logicSizeKind = "";
    private String logicSize = "";
    private String sramSize = "";
    private String ioCount = "";
    private String serdesCount = "";
    private String pkgType = "";
    private String quantity = "";
    private String[] price = new String[2];

    /**
     * Logic sizeの種類をセットする
     * @param logicSizeKind
     */
    public void setLogicSizeKind(String logicSizeKind){this.logicSizeKind = logicSizeKind;}

    /**
     * Logic sizeをセットする
     * @param logicSize
     */
    public void setLogicSize(String logicSize){this.logicSize = logicSize;}

    /**
     * SRAM sizeをセットする
     * @param sramSize
     */
    public void setSramSize(String sramSize){this.sramSize = sramSize;}

    /**
     * I/O countをセットする
     * @param ioCount
     */
    public void setIoCount(String ioCount){this.ioCount = ioCount;}

    /**
     * Serders countをセットする
     * @param serdesCount
     */
    public void setSerdesCount(String serdesCount){this.serdesCount = serdesCount;}

    /**
     * PKG-typeをセットする
     * @param pkgType
     */
    public void setPkgType(String pkgType){this.pkgType = pkgType;}

    /**
     * 数量をセットする
     * @param quantity
     */
    public void setQuantity(String quantity){this.quantity = quantity;}

    /**
     * 価格を取得する
     * @return 価格
     * @throws Exception
     */
    public String[] getPrice() throws Exception {

        // S2Container初期化
        SingletonS2ContainerFactory.init();

        // S2ContainerからJdbcManagerを取得
        JdbcManager jdbcManager = SingletonS2Container.getComponent(JdbcManager.class);

        try{
            // 検索条件
            String where[] = new String[2];

            // Logicによって検索条件を変える
            // M Gates
            if("1".equals(logicSizeKind)){
                where[0] = "   and equiv_asic >= ? \n";
            // Logic Cells
            }else if("2".equals(logicSizeKind)){
                where[0] = "   and equiv_lc >= ? \n";
            // Logic Element
            }else if("3".equals(logicSizeKind)){
                where[0] = "   and equiv_le >= ? \n";
            }

            // Quantityによって検索条件を変える
            // 5kp/year
            if(("1").equals(this.quantity)){
                where[1] = "   and price_5k = ( \n"
                         + "select min(cast(price_5k as BINARY)) \n"
                         + "  from price \n"
                         + " where pkg_type = ? \n"
                         + where[0]
                         + "   and ram_bits * 1000 >= ? \n"
                         + "   and max_io >= ? \n"
                         + "   and serdes >= ? \n"
                         + "   and price_5k != 'Contact us' \n"
                         + "   and price_5k != '-' \n"
                         + "   and price_5k is not null \n"
                         + "   and delete_flag = '0' \n"
                         + ") \n"
                         ;
            // 10kp/year
            }else if(("2").equals(this.quantity)){
                where[1] = "   and price_10k = ( \n"
                         + "select min(cast(price_10k as BINARY)) \n"
                         + "  from price \n"
                         + " where pkg_type = ? \n"
                         + where[0]
                         + "   and ram_bits * 1000 >= ? \n"
                         + "   and max_io >= ? \n"
                         + "   and serdes >= ? \n"
                         + "   and price_10k != 'Contact us' \n"
                         + "   and price_10k != '-' \n"
                         + "   and price_10k is not null \n"
                         + "   and delete_flag = '0' \n"
                         + ") \n"
                         ;
            // 20kp/year
            }else if(("3").equals(this.quantity)){
                where[1] = "   and price_20k = ( \n"
                         + "select min(cast(price_20k as BINARY)) \n"
                         + "  from price \n"
                         + " where pkg_type = ? \n"
                         + where[0]
                         + "   and ram_bits * 1000 >= ? \n"
                         + "   and max_io >= ? \n"
                         + "   and serdes >= ? \n"
                         + "   and price_20k != 'Contact us' \n"
                         + "   and price_20k != '-' \n"
                         + "   and price_20k is not null \n"
                         + "   and delete_flag = '0' \n"
                         + ") \n"
                         ;
            // 50kp/year
            }else if(("4").equals(this.quantity)){
                where[1] = "   and price_50k = ( \n"
                         + "select min(cast(price_50k as BINARY)) \n"
                         + "  from price \n"
                         + " where pkg_type = ? \n"
                         + where[0]
                         + "   and ram_bits * 1000 >= ? \n"
                         + "   and max_io >= ? \n"
                         + "   and serdes >= ? \n"
                         + "   and price_50k != 'Contact us' \n"
                         + "   and price_50k != '-' \n"
                         + "   and price_50k is not null \n"
                         + "   and delete_flag = '0' \n"
                         + ") \n"
                         ;
            // 100kp/year
            }else if(("5").equals(this.quantity)){
                where[1] = "   and price_100k = ( \n"
                         + "select min(cast(price_100k as BINARY)) \n"
                         + "  from price \n"
                         + " where pkg_type = ? \n"
                         + where[0]
                         + "   and ram_bits * 1000 >= ? \n"
                         + "   and max_io >= ? \n"
                         + "   and serdes >= ? \n"
                         + "   and price_100k != 'Contact us' \n"
                         + "   and price_100k != '-' \n"
                         + "   and price_100k is not null \n"
                         + "   and delete_flag = '0' \n"
                         + ") \n"
                        ;
            // 200kp/year
            }else if(("6").equals(this.quantity)){
                where[1] = "   and price_200k = ( \n"
                         + "select min(cast(price_200k as BINARY)) \n"
                         + "  from price \n"
                         + " where pkg_type = ? \n"
                         + where[0]
                         + "   and ram_bits * 1000 >= ? \n"
                         + "   and max_io >= ? \n"
                         + "   and serdes >= ? \n"
                         + "   and price_200k != 'Contact us' \n"
                         + "   and price_200k != '-' \n"
                         + "   and price_200k is not null \n"
                         + "   and delete_flag = '0' \n"
                         + ") \n"
                         ;
            // 200kp+/year
            }else if(("7").equals(this.quantity)){
                where[1] = "   and price_200k_plus = ( \n"
                         + "select min(cast(price_200k_plus as BINARY)) \n"
                         + "  from price \n"
                         + " where pkg_type = ? \n"
                         + where[0]
                         + "   and ram_bits * 1000 >= ? \n"
                         + "   and max_io >= ? \n"
                         + "   and serdes >= ? \n"
                         + "   and price_200k_plus != 'Contact us' \n"
                         + "   and price_200k_plus != '-' \n"
                         + "   and price_200k_plus is not null \n"
                         + "   and delete_flag = '0' \n"
                         + ") \n"
                         ;
            }

            // 価格情報取得
            List<Price> priceList = jdbcManager.selectBySql( Price.class
                                                           , "select * \n"
                                                           + "  from price \n"
                                                           + " where pkg_type = ? \n"
                                                           + where[0]
                                                           + "   and ram_bits * 1000 >= ? \n"
                                                           + "   and max_io >= ? \n"
                                                           + "   and serdes >= ? \n"
                                                           + where[1]
                                                           + "   and delete_flag = '0' \n"
                                                           , this.pkgType
                                                           , this.logicSize
                                                           , this.sramSize
                                                           , this.ioCount
                                                           , this.serdesCount
                                                           , this.pkgType
                                                           , this.logicSize
                                                           , this.sramSize
                                                           , this.ioCount
                                                           , this.serdesCount
                                                           )
                                                           .getResultList()
                                                           ;

            // データが取得できた場合
            if(priceList.size() > 0){
                // Quantityによって取得する価格を変える
                if(("1").equals(this.quantity)){
                    this.price[0] = priceList.get(0).price5k;
                }else if(("2").equals(this.quantity)){
                    this.price[0] = priceList.get(0).price10k;
                }else if(("3").equals(this.quantity)){
                    this.price[0] = priceList.get(0).price20k;
                }else if(("4").equals(this.quantity)){
                    this.price[0] = priceList.get(0).price50k;
                }else if(("5").equals(this.quantity)){
                    this.price[0] = priceList.get(0).price100k;
                }else if(("6").equals(this.quantity)){
                    this.price[0] = priceList.get(0).price200k;
                }else if(("7").equals(this.quantity)){
                    this.price[0] = priceList.get(0).price200kPlus;
                }

                // 価格が"-"の場合は"Contact us"をセットする
                if(("-").equals(this.price[0]) || ("").equals(this.price[0])){
                    this.price[0] = "Contact us";
                    this.price[1] = "";
                }else{
                    this.price[1] = "("
                                  + MessageResourcesUtil.getMessage("fpga_model")
                                  + ":"
                                  + priceList.get(0).fpgaModel
                                  + " "
                                  + MessageResourcesUtil.getMessage("pkg_type")
                                  + ":"
                                  + priceList.get(0).pkgType
                                  + " "
                                  + MessageResourcesUtil.getMessage("cses102.023")
                                  + ")"
                                  ;
                }
            // データが取得できなかった場合
            }else{
                this.price[0] = "Contact us";
                this.price[1] = "";
            }

            return price;

        }catch(Exception e){
            throw e;
        }
    }
}